package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.entity.ResidentialWorker;
import com.app.entity.userCar;
import com.app.exception.ValidateException;
import com.app.requestVO.StaffListRequestVO;
import com.app.requestVO.UserCarListRequestVO;
import com.app.responseVO.ReturnResponseVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
import com.app.service.StaffService;
import com.app.service.UserCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import java.io.IOException;

@Controller
public class UserCarController extends BaseController {
    @Autowired
    private UserCarService userCarService;

    /**
     * 新增车位
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "car/addUserCar", method = RequestMethod.POST)
    public void addUserCar(@RequestBody String body,
                           HttpServletResponse response) throws IOException {
        userCar userCar = JSON.parseObject(body, userCar.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            userCarService.addUserCar(userCar);
            userResponseVO.setMsg("成功");
            userResponseVO.setStatue(true);
        } catch (ValidateException e) {
            userResponseVO.setMsg(e.getMsg());
            userResponseVO.setStatue(false);
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 更新车位信息
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "car/updateCar", method = RequestMethod.POST)
    public void updateCar(@RequestBody String body,
                          HttpServletResponse response) throws IOException {
        userCar userCar = JSON.parseObject(body, userCar.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            userCarService.updateCar(userCar);
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 删除车位信息
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "car/deleteCar", method = RequestMethod.POST)
    public void deleteCar(@RequestBody String body,
                          HttpServletResponse response) throws IOException {
        userCar userCar = JSON.parseObject(body, userCar.class);
        UserResponseVO userResponseVO = new UserResponseVO();
        try {
            userCarService.deleteUserCar(userCar.getUserCarId());
            userResponseVO.setStatue(true);
            userResponseVO.setMsg("成功");
        } catch (ValidateException e) {
            userResponseVO.setStatue(false);
            userResponseVO.setMsg("失败");
        }
        writeJSON(response, userResponseVO);
    }

    /**
     * 获取车位列表
     *
     * @param body
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "car/getUserCarList", method = RequestMethod.POST)
    public void getUserCarList(@RequestBody String body,
                                HttpServletResponse response) throws IOException {
        ReturnResultResponseVO<userCar> returnResultResponseVO = new ReturnResultResponseVO<userCar>();
        try {
            UserCarListRequestVO userCarListRequestVO = JSON.parseObject(body, UserCarListRequestVO.class);
            if (null == userCarListRequestVO || null == userCarListRequestVO.getPageVO()) {
                throw new ValidationException("参数格式有误");
            }
            returnResultResponseVO = userCarService.getSuserCarList(userCarListRequestVO.getPageVO().getCurrentPage(), userCarListRequestVO.getPageVO().getPagesize(),
                    userCarListRequestVO.getUserCarName());
            returnResultResponseVO.setStatue(true);
        } catch (ValidationException e) {
            returnResultResponseVO.setStatue(false);
        }
        writeJSON(response, returnResultResponseVO);
    }
    @RequestMapping(value = "car/getUserCarPageList")
    @ResponseBody
    public JSONObject getUserCarPageList(userCar userCar,@RequestParam("page") int currentPage,@RequestParam("limit") int pagesize,
    		HttpServletResponse response) throws IOException {
    	ReturnResultResponseVO<userCar> returnResultResponseVO = new ReturnResultResponseVO<userCar>();
    	JSONObject jsonObject = new JSONObject();  
    	try {
    		
    		returnResultResponseVO = userCarService.getSuserCarPageList(currentPage, pagesize, userCar.getUserName());
    		returnResultResponseVO.setStatue(true);
    		jsonObject.put("count", returnResultResponseVO.getPage().getPagesize());  
			jsonObject.put("data", returnResultResponseVO.getData()); 
    	} catch (ValidationException e) {
    		returnResultResponseVO.setStatue(false);
    	}
    	jsonObject.put("code", 0);  
		jsonObject.put("msg", "");  
    	//writeJSON(response, returnResultResponseVO);
    	return jsonObject;
    }

    /**
     * 获取车位详情
     *
     * @param body
     * @param response
     */
    @RequestMapping(value = "car/getCarDetail", method = RequestMethod.POST)
    public void getCarDetail(@RequestBody String body,
                             HttpServletResponse response) throws IOException {
        userCar userCar = JSON.parseObject(body, userCar.class);
        userCar car = userCarService.userCarDetail(userCar.getUserCarId());
        ReturnResponseVO<userCar> returnResponseVO = new ReturnResponseVO<userCar>();
        returnResponseVO.setData(car);
        returnResponseVO.setStatue(true);
        returnResponseVO.setMsg("成功");
        writeJSON(response, returnResponseVO);
    }
}
